package com.joplus.housekeeper.bean;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMImageMessageBody;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.chat.EMVoiceMessageBody;
import com.hyphenate.chatuidemo.DemoHelper;
import com.joplus.housekeeper.core.Config;

import java.io.File;
import java.util.UUID;

import io.realm.RealmModel;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

/**
 * Created by zisheng on 2017/8/31.
 * type:1文本  2语音  3图片 4 普通表情  5 url 文件  8gif  9 卡片
 * duration 0 发送 1 接受
 */

public class XmppMessage extends RealmObject {
    @PrimaryKey
    private String messageId;

    private String content;
    private String status;
    private Long messageDateTime;
    private String type;
    private int duration;
    private String messageFrom;
    private String from;
    private String to;
    private String avatar;
    private int direct;
    private String name;
    private String accoutId;
    private String gender;


    private String memberId;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAccoutId() {
        return accoutId;
    }

    public String getMemberId() {
        return memberId;
    }

    public void setMemberId(String memberId) {
        this.memberId = memberId;
    }

    public void setAccoutId(String accoutId) {
        this.accoutId = accoutId;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public int getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public String getMessageFrom() {
        return messageFrom;
    }

    public void setMessageFrom(String messageFrom) {
        this.messageFrom = messageFrom;
    }

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Long getMessageDateTime() {
        return messageDateTime;
    }

    public void setMessageDateTime(Long messageDateTime) {
        this.messageDateTime = messageDateTime;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public EMMessage change2EmMessage() {
        if (getType().equals("1")) {
            if (getDirect() == 0) {
                EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
                message.setFrom(getFrom());
                message.setTo(getTo());
                message.addBody(new EMTextMessageBody(getContent()));
                message.setChatType(EMMessage.ChatType.Chat);
                message.setMsgTime(getMessageDateTime());
                message.setMsgId(getMessageId());

                return message;
            } else {
                EMMessage message = EMMessage.createReceiveMessage(EMMessage.Type.TXT);
                message.setFrom(getFrom());
                message.setTo(getTo());
                message.addBody(new EMTextMessageBody(getContent()));
                message.setChatType(EMMessage.ChatType.Chat);
                message.setMsgTime(getMessageDateTime());
                message.setMsgId(getMessageId());

                return message;
            }
        } else if (getType().equals("2")) {
            EMMessage voiceSendMessage;
            if (getDirect() == 0) {
                voiceSendMessage = EMMessage.createVoiceSendMessage(getContent(), getDuration(), getTo());
            } else {
                voiceSendMessage = createReceiveMessage(getContent(), getDuration(), getTo());
            }

            voiceSendMessage.setChatType(EMMessage.ChatType.Chat);
            voiceSendMessage.setFrom(getFrom());
            voiceSendMessage.setTo(getTo());
            if (getMessageDateTime() != null) {
                voiceSendMessage.setMsgTime(getMessageDateTime());
            }
            return voiceSendMessage;

        } else if (getType().equals("3")) {
            EMMessage message;
            if (getDirect() == 0) {
                message = EMMessage.createSendMessage(EMMessage.Type.IMAGE);
            } else {
                message = EMMessage.createReceiveMessage(EMMessage.Type.IMAGE);
            }
            message.setFrom(getFrom());
            message.setTo(getTo());
            message.addBody(new EMImageMessageBody(new File(getContent())));
            message.setChatType(EMMessage.ChatType.Chat);
            if (getMessageDateTime() != null) {
                message.setMsgTime(getMessageDateTime());
            }
            return message;
        } else if (getType().equals("8")) {
            if (getDirect() == 0) {
                EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
                message.setFrom(getFrom());
                message.setTo(getTo());
                message.addBody(new EMTextMessageBody(getContent()));
                message.setChatType(EMMessage.ChatType.Chat);
                message.setMsgTime(getMessageDateTime());
                message.setMsgId(getMessageId());

                return message;
            } else {
                EMMessage message = EMMessage.createReceiveMessage(EMMessage.Type.TXT);
                message.setFrom(getFrom());
                message.setTo(getTo());
                message.addBody(new EMTextMessageBody(getContent()));
                message.setChatType(EMMessage.ChatType.Chat);
                message.setMsgTime(getMessageDateTime());
                message.setMsgId(getMessageId());

                return message;
            }
        }
        return null;
    }

    public EMMessage createReceiveMessage(String filePath, int length, String toChatUsername) {
        EMMessage receiveMessage = EMMessage.createReceiveMessage(EMMessage.Type.VOICE);
        EMVoiceMessageBody emVoiceMessageBody = new EMVoiceMessageBody(new File(filePath), length);
        receiveMessage.addBody(emVoiceMessageBody);
        receiveMessage.setTo(toChatUsername);
        receiveMessage.setChatType(EMMessage.ChatType.Chat);
        receiveMessage.setFrom(Config.CUSTOM_SERVICE_IMID);
        receiveMessage.setMsgTime(System.currentTimeMillis());
        return receiveMessage;
    }
}
